package top.i51213.fastaction.uri;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;

import androidx.annotation.NonNull;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;

import top.i51213.fastaction.util.IOUtil;

public class UriReader {
    private final WeakReference<Context> context;
    private final Uri uri;
    private String disPlayName;
    private File file;

    public UriReader(Context context, Uri uri) {
        this.context = new WeakReference<>(context);
        this.uri = uri;
        if ("content".equals(uri.getScheme())) {
            queryContentUri();
        } else if ("file".equals(uri.getScheme())) {
            queryFileUri();
        }
    }

    public static UriReader fromFile(Context context, File file) {
        return new UriReader(context, Uri.fromFile(file));
    }

    public Context getContext() {
        return context.get();
    }

    public Uri getUri() {
        return uri;
    }

    public String getDisPlayName() {
        return disPlayName;
    }

    @NonNull
    public String getDisPlayName( @NonNull String ext) {
        return disPlayName != null ? disPlayName : (IOUtil.getRandomTime()+ ext);
    }

    public File getFile() {
        return file;
    }

    @NonNull
    @Override
    public String toString() {
        return "UriReader{" +
                "context=" + context +
                ", uri=" + uri +
                ", disPlayName='" + disPlayName + '\'' +
                ", file=" + file +
                '}';
    }

    private void queryContentUri() {
        String[] filePathColumn = {MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME};
        ContentResolver contentResolver = getContext().getContentResolver();
        Cursor cursor = contentResolver.query(uri, filePathColumn, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
            int index = cursor.getColumnIndex(filePathColumn[0]);
            //这里有可能为空 所以要做判断
            if (index > -1) {
                String filePath = cursor.getString(index);
                if (filePath != null) {
                    file = new File(filePath);
                }
            }
            disPlayName = cursor.getString(cursor.getColumnIndex(filePathColumn[1]));
            cursor.close();
        }
    }

    private void queryFileUri() {
        file = new File(uri.getPath());
        disPlayName = file.getName();
    }

    public InputStream openInputStream() throws IOException {
        if ("content".equals(uri.getScheme())) {
            return getContext().getContentResolver().openInputStream(uri);
        } else if ("file".equals(uri.getScheme())) {
            return new FileInputStream(uri.getPath());
        } else {
            throw new IOException("不支持的Uri协议");
        }
    }

    public ParcelFileDescriptor.AutoCloseInputStream openAutoCloseInputStream() throws IOException {
        if ("content".equals(uri.getScheme())) {
            return new ParcelFileDescriptor.AutoCloseInputStream(getContext().getContentResolver().openFileDescriptor(uri, "r"));
        } else if ("file".equals(uri.getScheme())) {
            return new ParcelFileDescriptor.AutoCloseInputStream(ParcelFileDescriptor.open(new File(uri.getPath()), ParcelFileDescriptor.MODE_READ_ONLY));
        } else {
            throw new IOException("不支持的Uri协议");
        }
    }

    public ParcelFileDescriptor openParcelFileDescriptor() throws IOException {
        if ("content".equals(uri.getScheme())) {
            return getContext().getContentResolver().openFileDescriptor(uri, "r");
        } else if ("file".equals(uri.getScheme())) {
            return ParcelFileDescriptor.open(new File(uri.getPath()), ParcelFileDescriptor.MODE_READ_ONLY);
        } else {
            throw new IOException("不支持的Uri协议");
        }
    }
}
